'use strict';

/**
 * page service
 */
angular.module('sfhtHaitaoCmsApp')

	.service('TopicService', function($q, Gateway, $injector){
    	// 通用的request方法返回promise对象
		var request = function (api, params, isForceUserLogin, cms) {

			var deferred = $q.defer();

			var api = $injector.get(api);

			var gateway = new Gateway(api, params);

			gateway.sendRequest(false, true)
				.then(function(data) {
					deferred.resolve(data);
				}, function(data) {
					deferred.reject(data);
				});

			return deferred.promise;
		};

		/**
		 * 将所有接口做封装，便于接口管控
		 */
		return {

			/**
			 * 查询专题列表
			 * @param  {Boolean} 是否只查询我编辑的页面
			 * @param  {string} 搜索关键词
			 * @param  {int} 分页的页码
			 * @param  {int} 页面类型0:pc,1:h5
			 * @return {array}
			 */
			getTopicList: function(periodOptions, isOnlyShowMine, searchKeywords, pageType, pagedQueryOption, showAbandoned) {

				var params = {
					type: pageType,
					periodOptions: periodOptions,
					isOnlyShowMine: isOnlyShowMine,
					searchKeywords: searchKeywords,
					pagedQueryOption: JSON.stringify(pagedQueryOption),
					showAbandoned: showAbandoned
				};

				return request('sf_b2c_mall_api_cms_getTopicList', params, false, true);
			},

			/**
			 * 设置是否关注
			 * @param  {int} 专题ID
			 * @param  {boolean} 是否关注：0：未关注，1：已关注
			 * @return {array}
			 */
			setWatch: function(topicId, isWatch) {

				var params = {
					id: topicId,
					isWatch: isWatch
				};

				return request('sf_b2c_mall_api_cms_setWatch', params, false, true);
			},

			/**
			 * 删除专题
			 * @param  {int} 专题ID
			 * @param  {boolean} 确认是否删除
			 * @return {array}
			 */
			setTopicDelete: function(topicId, isDelete) {

				var params = {
					id: topicId,
					isDelete: isDelete
				};

				return request('sf_b2c_mall_api_cms_setTopicDelete', params, false, true);
			},

			/**
			 * 新建专题
			 * @param  {string} 页面名称
			 * @return {array}
			 */
			saveTopic: function(pageId, startTime, endTime, bannerImg, status) {

				var params = {
					pageId: pageId,
					startTime: startTime,
					endTime: endTime,
					bannerImg:bannerImg,
					status:status
				};

				return request('sf_b2c_mall_api_cms_createTopic', params, false, true);
			},

			/**
			 * 修改专题属性
			 * @param  {object} 专题对象
			 * @return {array}
			 */
			updateTopic: function(topic) {

				var params = {
					cmsTopic: JSON.stringify(topic)
				};

				return request('sf_b2c_mall_api_cms_updateTopic', params, false, true);
			},

			getSpecialPageList: function() {

				var params = {};

				return request('sf_b2c_mall_api_cms_getSpecialPageList', params, false, true);
			},

			updateSepcialPage: function(type, uri, pageId) {

				var params = {
					type: type,
					uri: uri,
					pageId: pageId
				};

				return request('sf_b2c_mall_api_cms_updateSepcialPage', params, false, true);
			}
		};
    });